<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\Store;
use App\Models\Admin\Grade;
use App\Models\Admin\Share;
use App\Models\Admin\TeamAward;
use Config, Auth;;


class StoreController extends Controller
{
    // 店铺信息读取
    public function storeInfo(Request $request)
    {
        // 读取店铺ID
        $user = Auth::guard('admin')->user();
        if ($user->store_id === 0) {
            // 超管必需选择店铺
            $id = $request->id;
            if (empty($id)) {
                return self::errorClient(['info' => '请输入店铺ID']);
            }
        } else {
            // 否则 获取用户本身代表的店铺
            $id = $user->store_id;
        }
        $Store = new Store;
        $Grade = new Grade;
        $Share = new Share;
        $TeamAward = new TeamAward;
        // 获取店铺信息
        $store = $Store->getStoreText($id);
        // return $store;
        if (empty($store['id'])) {
            return self::errorClient(['info' => '店铺不存在']);
        }
        // 获取会员等级信息 - 传递店铺ID
        $grade = $Grade->getGradeText($id);
        if (empty($grade)) {
            return self::errorClient(['info' => '店铺等级未设置']);
        }
        // 获取分享经济信息
        $share = $Share->getShareText($id, $grade['grade']);
        if (empty($share)) {
            return self::errorClient(['info' => '店铺分享经济信息未设置']);
        }
        // 获取团队奖励信息
        $team_award = $TeamAward->getTeamAwardText($id, $grade['grade']);
        if (empty($team_award)) {
            return self::errorClient(['info' => '店铺团队奖励未设置']);
        }
        $data = [];
        $data['store'] = $store;
        $data['grade'] = $grade;
        $data['share'] = $share;
        $data['team_award'] = $team_award;
        return self::successClient($data);
    }

    // 店铺列表 初始化
    public function storeList(Request $request)
    {
        // 获取店铺列表 并 获取店铺ID 数组
        $Store = new Store;
        $store = $Store->getList($request);
        $store_id_arr = array_column($store['data'],'id');

        // return $store_id_arr;
        //遍历店铺 确认是否设置对应模块
        $data_end = $Store->storeCheck($store,$store_id_arr);
        return ($data_end);
    }

    // 店铺状态修改
    public function storeStatus(Request $request)
    {
        $status = $request['status'];
        $id = $request['id'];
        $store_status = Config::get("const.store_status");
        if (!in_array($status,$store_status)) {
            return self::errorClient(['info' => '状态不合法']);
        }

        $Store = new Store;
        $change_status=$Store->changeStatus($id, $status);

        return $change_status;
    }

    // 提交店铺信息 - 通过是否 传递 店铺ID , 更新 或 插入
    public function storeSubmit(Request $request)
    {
        $id = empty($request['id']) ? 0 : $request['id'];

        $Store = new Store;
        $data = $id == 0 ? $Store->insertStore($request) : $Store->updateStore($request);
        return $data;
    }

    // 判断店铺信息是否存在
}
